﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
namespace ADB_QuanLyThuVien
{
    public partial class frmTraCuuSach : DevExpress.XtraEditors.XtraForm
    {
        private List<Book> lBook;
        private List<Book> lBookRequest = new List<Book>();
        private Account acc;
        private int sua;
        private List<Book> list;
        Member reader = new Member();
        private List<BookRequest> lBookRequest1 = new List<BookRequest>();
        private frmTrangChu frmTrangChu;
        private List<BookRequest> lBookNotBorrow;
        
        public frmTraCuuSach()
        {
            InitializeComponent();
        }

        public frmTraCuuSach(Account acc)
        {
            this.acc = acc;
            InitializeComponent();

        }

        public frmTraCuuSach(List<Book> list)
        {
            // TODO: Complete member initialization
            this.list = list;
        }

        public frmTraCuuSach(List<Book> list, Account acc, frmTrangChu frmTrangChu)
        {
            // TODO: Complete member initialization
            this.lBookRequest = list;
            this.acc = acc;
            InitializeComponent();
            this.frmTrangChu = frmTrangChu;
            chkMuonSach.Checked = true;
        }

        public frmTraCuuSach(List<Book> lBook, Account acc, Member dg, frmTrangChu frmTrangChu)
        {
            // TODO: Complete member initialization
            this.lBookRequest = lBook;
            this.acc = acc;
            this.reader = dg;
            InitializeComponent();
            this.frmTrangChu = frmTrangChu;
            chkMuonSach.Checked = true;
        }

        public frmTraCuuSach(List<BookRequest> lBookRQ, Account acc, frmTrangChu frmTrangChu)
        {
            // TODO: Complete member initialization
            if (lBookRQ != null)
            {
                this.lBookRequest1 = lBookRQ;
                this.lBookRequest = new List<Book>();
                for (int i = 0; i < lBookRQ.Count; ++i)
                {
                    if (lBookRQ[i].IsBorrow == false)
                        this.lBookRequest.Add(lBookRQ[i].bookRequest);
                }
            }
            this.acc = acc;
            this.frmTrangChu = frmTrangChu;
            InitializeComponent();
            chkMuonSach.Checked = true;
        }

        public frmTraCuuSach(Account acc, frmTrangChu frmTrangChu)
        {
            // TODO: Complete member initialization
            this.acc = acc;
            this.frmTrangChu = frmTrangChu;
            InitializeComponent();
        }

        private void ShowSearch(bool show)
        {
            if (show == true)
            {
                grpTimKiem.Visible = true;
                chkBookName.Checked = false;
                chkAuthor.Checked = false;
                chkCategory.Checked = false;
                chkPublisher.Checked = false;
            }
            else
                grpTimKiem.Visible = false;
        }
        private void rdb_All_CheckedChanged(object sender, EventArgs e)
        {
            if (rdb_All.Checked == true)
                ShowSearch(false);
            else
                ShowSearch(true);
        }

        private void frmTraCuuSach_Load(object sender, EventArgs e)
        {
            rdb_All.Checked = true;
            ShowSearch(false);
            txt_tktensach.Enabled = false;
            txt_tktensach.Text = "";
            cbb_tkTacgia.Enabled = false;
            cbb_tkTacgia.Text = "";
            cbb_tkNxb.Enabled = false;
            cbb_tkNxb.Text = "";
            cbb_tktheloai.Enabled = false;
            cbb_tktheloai.Text = "";
            grpSort.Visible = false;
            chkMuonSach.Checked = true;
            // Hiển thị button đăng ký sách mượn dành cho độc giả hay form mượn sách dành cho nhân viên 
            //(độc giả mượn sách tại thư viện không đăng ký qua online)
            if (acc == null)
            {
                pEmployee.Visible = false;
                pReader.Visible = false;
            }
            else
                if (DAO_Member.isEmployee(acc))
                {
                    pEmployee.Visible = true;
                    pReader.Visible = false;
                }
                else
                {
                    pEmployee.Visible = false;
                    pReader.Visible = true;
                }
            if (lBookRequest.Count > 0)
            {
                bookBindingSource1.DataSource = lBookRequest;
                SetNumberColumn(bookDataGridView1);
            }
        }

        private void DanhSoTT(DataGridView data)
        {
            for (int i = 0; i < data.RowCount; ++i)
                data.Rows[i].Cells[0].Value = (i + 1).ToString();
        }

        private void chkBookName_CheckedChanged(object sender, EventArgs e)
        {
            if (chkBookName.Checked == false)
            {
                txt_tktensach.Enabled = false;
                txt_tktensach.Text = "";
            }
            else
                txt_tktensach.Enabled = true;
        }

        private void chkAuthor_CheckedChanged(object sender, EventArgs e)
        {
            if (chkAuthor.Checked == false)
            {
                cbb_tkTacgia.Enabled = false;
                cbb_tkTacgia.Text = "";
            }
            else
            {
                cbb_tkTacgia.Enabled = true;
                List<Author> lAuthor = new List<Author>();
                lAuthor = DAO_Authors.SelectAll();
                foreach (var item in lAuthor)
                {
                    cbb_tkTacgia.Properties.Items.Add(item.Name);
                }
            }
        }

        private void chkPublisher_CheckedChanged(object sender, EventArgs e)
        {
            if (chkPublisher.Checked == false)
            {
                cbb_tkNxb.Enabled = false;
                cbb_tkNxb.Text = "";
            }
            else
            {
                cbb_tkNxb.Enabled = true;
                List<DTO_NhaXuatBan> lPublisher = new List<DTO_NhaXuatBan>();
                lPublisher = DAO_Publisher.SelectAll();
                foreach (var item in lPublisher)
                {
                    cbb_tkNxb.Properties.Items.Add(item.TenNXB);
                }
            }
        }

        private void chkCategory_CheckedChanged(object sender, EventArgs e)
        {
            if (chkCategory.Checked == false)
            {
                cbb_tktheloai.Enabled = false;
                cbb_tktheloai.Text = "";
            }
            else
            {
                cbb_tktheloai.Enabled = true;
                List<Category> lCategory = new List<Category>();
                lCategory = DAO_Categories.SelectAll();
                foreach (var item in lCategory)
                {
                    cbb_tktheloai.Properties.Items.Add(item.NameCa);
                }
            }
        }

        private void chkSort_CheckedChanged(object sender, EventArgs e)
        {
            if (chkSort.Checked == false)
            {
                grpSort.Visible = false;
            }
            else
                grpSort.Visible = true;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (rdb_All.Checked == true)
            {
                lBook = DAO_Books.SelectAll();
            }
            else
            {
                Book sach = new Book();
                string tenSach = null;
                if (chkBookName.Checked == true)
                    tenSach = txt_tktensach.Text;
                if (chkAuthor.Checked == true)
                {
                    string auname = cbb_tkTacgia.SelectedText.ToString();
                    sach.Tacgia = DAO_Authors.GetAuthorByName(auname);
                }
                if (chkPublisher.Checked == true)
                {
                    string puname = cbb_tkNxb.SelectedText.ToString();
                    sach.NhaXB = DAO_Publisher.GetPublisherByName(puname);
                }
                if (chkCategory.Checked == true)
                {
                    string caname = cbb_tktheloai.SelectedText.ToString();
                    sach.TheLoai = DAO_Categories.GetCategoryByName(caname);
                }
                lBook = DAO_Books.SelectBooks(sach, tenSach);
            }
            if (lBook.Count == 0)
                XtraMessageBox.Show("Không có sách tương ứng...");
            if (chkSort.Checked == true)
                if (rdbLuot.Checked == true)
                    lBook.Sort();
                else
                    lBook.Sort(new sortOnNamXB());
            bookBindingSource.DataSource = lBook;
            SetNumberColumn(bookDataGridView);
            SetValueChonColumn(bookDataGridView);
            SetChonColumn(bookDataGridView);
            
        }

        private void SetValueChonColumn(DataGridView bookDataGridView)
        {
                for (int i = 0; i < bookDataGridView.RowCount; ++i)
                    bookDataGridView.Rows[i].Cells[1].Value = "0";
        }

        private void SetChonColumn(DataGridView bookDataGridView)
        {
            for (int i = 0; i < lBookRequest.Count; ++i)
                for (int j = 0; j < lBook.Count; ++j)
                    if (lBookRequest[i].MaSach == lBook[j].MaSach)
                    {
                        bookDataGridView.Rows[j].Cells[1].Value = "1";
                        break;
                    }
        }

        private void SetNumberColumn(DataGridView gridView)
        {
            for (int i = 0; i < gridView.RowCount; ++i)
                gridView.Rows[i].Cells[0].Value = (i + 1).ToString();
        }

        private void rdbLuot_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                bookBindingSource.DataSource = new List<Book>();
                if (rdbLuot.Checked == true)
                    lBook.Sort();
                else
                    lBook.Sort(new sortOnNamXB());
                bookBindingSource.DataSource = lBook;
                QLTV_System.SetNumberColumn(bookDataGridView);
            }
            catch
            {
                MessageBox.Show("Bạn chưa tìm kiếm không thể sắp xếp được...");
            }
        }

        private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            int rowIndex = e.RowHandle;
            if (rowIndex >= 0)
            {
                rowIndex++;
                e.Info.DisplayText = rowIndex.ToString();
            }
        }

        private void checkMuonSach_CheckedChanged(object sender, EventArgs e)
        {
            if (chkMuonSach.Checked == true)
            {
                btnDangKy.Visible = true;
                bookDataGridView.Columns["colMuonSach"].Visible = true;
            }
            else
            {
                btnDangKy.Visible = false;
                bookDataGridView.Columns["colMuonSach"].Visible = false;
            }

        }

        private void btnDangKy_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < lBookRequest.Count; ++i)
            {
                int j;
                for (j = 0; j < lBookRequest1.Count; ++j)
                    if (lBookRequest1[j].bookRequest == lBookRequest[i])
                        break;
                if (j == lBookRequest1.Count)
                {
                    BookRequest brq = new BookRequest();
                    brq.bookRequest = lBookRequest[i];
                    brq.DateRequest = DateTime.Today;
                    brq.IsBorrow = false;
                    lBookRequest1.Add(brq);
                }
            }
            for (int i = 0; i < lBookRequest1.Count; ++i)
            {
                int j;
                for (j = 0; j < lBookRequest.Count; ++j)
                    if (lBookRequest1[i].bookRequest == lBookRequest[j])
                        break;
                if (j == lBookRequest.Count)
                {
                    lBookRequest1.RemoveAt(i);
                }
            }
            //var frm = new frmDangKyMuonSach(lBookRequest1, acc);
            //frm.Show();
            //this.Hide();
            string tabTittle = "Đăng ký mượn"; //Tieu de cua Tab
            DevExpress.XtraTab.XtraTabPage tab = new DevExpress.XtraTab.XtraTabPage();
            tab.Text = tabTittle;
            for (int i = 0; i < frmTrangChu.xtraTabControl1.TabPages.Count; i++)
            {
                if (frmTrangChu.xtraTabControl1.TabPages[i].Text == tabTittle)
                {
                    frmTrangChu.xtraTabControl1.TabPages.Remove(frmTrangChu.xtraTabControl1.TabPages[i]);
                    break;
                }
            }
            frmTrangChu.xtraTabControl1.TabPages.Add(tab);
            var hien = new frmDangKyMuonSach(lBookRequest1, acc, frmTrangChu);
            hien.TopLevel = false;
            hien.Dock = DockStyle.Fill;
            tab.Controls.Add(hien);
            hien.Show();
            frmTrangChu.xtraTabControl1.SelectedTabPageIndex = frmTrangChu.xtraTabControl1.TabPages.Count - 1;
        }

        private void RemoveBookIntoListRequestBook(Book book)
        {
            int i;
            for (i = 0; i < lBookRequest.Count; ++i)
                if (lBookRequest[i].MaSach == book.MaSach)
                {
                    lBookRequest.RemoveAt(i);
                    break;
                }
            bookDataGridView1.DataSource = new List<Book>();
            bookDataGridView1.DataSource = lBookRequest;
            SetNumberColumn(bookDataGridView1);
        }

        private void AddBookIntoListRequestBook(Book book)
        {
            int i;
            for (i = 0; i < lBookRequest.Count; ++i)
                if (lBookRequest[i].MaSach == book.MaSach)
                    break;
            if (i == lBookRequest.Count)
                lBookRequest.Add(book);
            bookDataGridView1.DataSource = new List<Book>();
            bookDataGridView1.DataSource = lBookRequest;
            SetNumberColumn(bookDataGridView1);
        }

        private void btnMuonSach_Click(object sender, EventArgs e)
        {
            //var frm = new frmPhieuMuonSach(acc, lBookRequest, reader);
            //frm.Show();
            //this.Hide();

            string tabTittle = "Mượn sách"; //Tieu de cua Tab
            DevExpress.XtraTab.XtraTabPage tab = new DevExpress.XtraTab.XtraTabPage();
            tab.Text = tabTittle;
            for (int i = 0; i < frmTrangChu.xtraTabControl1.TabPages.Count; i++)
            {
                if (frmTrangChu.xtraTabControl1.TabPages[i].Text == tabTittle)
                {
                    frmTrangChu.xtraTabControl1.TabPages.Remove(frmTrangChu.xtraTabControl1.TabPages[i]);
                    break;
                }
            }
            frmTrangChu.xtraTabControl1.TabPages.Add(tab);
            var hien = new frmPhieuMuonSach(acc, lBookRequest, reader, frmTrangChu);
            hien.TopLevel = false;
            hien.Dock = DockStyle.Fill;
            tab.Controls.Add(hien);
            hien.Show();
            frmTrangChu.xtraTabControl1.SelectedTabPageIndex = frmTrangChu.xtraTabControl1.TabPages.Count - 1;

        }
        
        private void bookDataGridView_Click(object sender, EventArgs e)
        {
            if (bookDataGridView.CurrentCell.ColumnIndex == 1)
                if (bookDataGridView.Rows[bookDataGridView.CurrentRow.Index].Cells[1].Value == "0")
                {
                    AddBookIntoListRequestBook(lBook[bookDataGridView.CurrentRow.Index]);
                    bookDataGridView.Rows[bookDataGridView.CurrentRow.Index].Cells[1].Value = "1";
                }
                else
                {
                    bookDataGridView.Rows[bookDataGridView.CurrentRow.Index].Cells[1].Value = "0";
                    RemoveBookIntoListRequestBook(lBook[bookDataGridView.CurrentRow.Index]);
                }
        }
    }
}